#include "calculate.h"
int cal(QString str)
{
    string sp;
        int kuohaoNum=0;
        int spLength;
        int result=0;
        vector<rec> recs;
        vector<int> reci;
            recs.clear();
            reci.clear();
            int signNum=0;
            char* p=new char[100];
            char* ptmp=p;

            QByteArray ba=str.toLatin1();
            const char *s_=ba.data();
            p=const_cast<char*>(s_);

            sp=string(p);
            rec rj;

            spLength=sp.length();


            int spNum=atoi(ptmp);
            reci.push_back(spNum);
            for(int i=0; i<spLength; i++)
            {
                if(*ptmp>='0'&&*ptmp<='9')
                {
                    ptmp++;
                }
                else
                {
                    switch(sp[i])
                    {
                    case '(':
                        kuohaoNum++;
                        ptmp++;
                        if(*ptmp>='0'&&*ptmp<='9')
                        {
                            spNum=atoi(ptmp);
                            signNum++;
                            reci.push_back(spNum);
                        }
                        break;
                    case ')':
                        kuohaoNum--;
                        ptmp++;
                        if(*ptmp>='0'&&*ptmp<='9')
                        {
                            spNum=atoi(ptmp);
                            signNum++;
                            reci.push_back(spNum);
                        }
                        break;
                    case '+':
                        rj.pos=signNum;
                        rj.sign='+',rj.pri=0+2*kuohaoNum;
                        recs.push_back(rj);
                        ptmp++;
                        if(*ptmp>='0'&&*ptmp<='9')
                        {
                            spNum=atoi(ptmp);
                            signNum++;
                            reci.push_back(spNum);
                        }
                        break;
                    case '-':
                        rj.pos=signNum;
                        rj.sign='-',rj.pri=0+2*kuohaoNum;
                        recs.push_back(rj);
                        ptmp++;
                        if(*ptmp>='0'&&*ptmp<='9')
                        {
                            spNum=atoi(ptmp);
                            signNum++;
                            reci.push_back(spNum);
                        }
                        break;
                    case '*':
                        rj.pos=signNum;
                        rj.sign='*',rj.pri=1+2*kuohaoNum;
                        recs.push_back(rj);
                        ptmp++;
                        if(*ptmp>='0'&&*ptmp<='9')
                        {
                            spNum=atoi(ptmp);
                            signNum++;
                            reci.push_back(spNum);
                        }
                        break;
                    case '/':
                        rj.pos=signNum;
                        rj.sign='/',rj.pri=1+2*kuohaoNum;
                        recs.push_back(rj);
                        ptmp++;
                        if(*ptmp>='0'&&*ptmp<='9')
                        {
                            spNum=atoi(ptmp);
                            signNum++;
                            reci.push_back(spNum);
                        }
                        break;
                    }

                }
            }
            vector<int>::iterator iter=reci.begin();
            while(iter!=reci.end())
            {
                ++iter;
            }
            delete [] p;
            p=NULL;

            int max;
            int min;
            for(int i=0; i<recs.size(); i++)
            {
                max=i;
                for(int j=i+1; j<recs.size(); j++)
                {
                    if(recs[max].pri<recs[j].pri)
                        max=j;
                }
                if(i!=max)
                {
                    rec temp=recs[i];
                    recs[i]=recs[max];
                    recs[max]=temp;
                }
            }
            for(int i=0; i<recs.size(); i++)
            {
                min=i;
                if(recs[i].pri==0)
                {
                    for(int j=i+1; j<recs.size(); j++)
                    {
                        if(recs[min].pos>recs[j].pos)
                            min=j;
                    }
                    if(i!=max)
                    {
                        rec temp=recs[i];
                        recs[i]=recs[min];
                        recs[min]=temp;
                    }
                }
            }
            for(int i=0; i<recs.size(); i++)
            {
                rec rw=recs.at(i);
            }
            int i=0;
            while(signNum--)
            {
                switch(recs.at(i).sign)
                {
                case '+':
                    reci[recs.at(i).pos]=reci.at(recs.at(i).pos)+reci.at(recs.at(i).pos+1);
                    break;
                case '-':
                    reci[recs.at(i).pos]=reci.at(recs.at(i).pos)-reci.at(recs.at(i).pos+1);
                    break;
                case '*':
                    reci[recs.at(i).pos]=reci.at(recs.at(i).pos)*reci.at(recs.at(i).pos+1);
                    break;
                case '/':
                    reci[recs.at(i).pos]=reci.at(recs.at(i).pos)/reci.at(recs.at(i).pos+1);
                    break;
                }
                for(int j=i+1; j<recs.size(); j++)
                {
                    if(recs.at(j).pos>recs.at(i).pos)
                        recs[j].pos--;
                }
                iter=reci.begin();
                iter+=recs.at(i).pos+1;
                reci.erase(iter,iter+1);
                iter=reci.begin();
                while(iter!=reci.end())
                {
                    result=*iter;
                    ++iter;
                }
                i++;
            }
            return result;
}
